<#include '/admin/header.html' >
<#assign dateformat="com.enation.framework.directive.DateformateDirective"?new()/> 
<script type="text/javascript" src="${ctx}/statics/e_tools/js/jquery.goodsSelector.js"> </script>
<script type="text/javascript" src="${ctx}/shop/admin/js/jquery.autoFloat.js"> </script>
<style>
	.layui-form-pane .layui-form-checkbox{padding:0 35px 0 0;margin:0px 20px 0 10px;height:38px;line-height:36px;float:left;}
	.same-acrivity-box{width:100%;height:40px;margin:0 0 15px 0;display:block;}
	#add_form{padding:0 0 30px 0;}
	.same-acrivity-detail a{height:38px;line-height:38px;display:inline-block;}
.layer-anim{
	height:500px!important;
	}
	.layui-layer-page .layui-layer-content{
	height:400px!important;
	}
	.layui-form-item{
	margin-left:10px;
	}
	.__GSR__ .dataTables_paginate .paginate_button, .__GSR__ .dataTables_paginate .ellipsis{
	padding:0px 0px;
	}
	.__GSR__ .layui-table th, .__GSR__ .layui-table td{
	padding:8px 35px;
	}
	.pagination>li>a, .pagination>li>span{
	margin-left:-1px;
	padding:4px 10px;
	}
	.pagination>li:first-child>a, .pagination>li:first-child>span{
	    margin-left: -3px;
	}
	.pagination>li:last-child>a, .pagination>li:last-child>span{
	margin-right:-3px;
	}
  
</style>

<div class="admin-main" style="background-color: white;">
	<form id="edit_form" class="layui-form layui-form-pane" action="" enctype="multipart/form-data" style="padding-top:10px">
	<input type="hidden" name="activity_id" value="${activity.activity_id}">
	<div class="layui-form-item">
    	<label class="layui-form-label">活动名称：</label>
    	<div class="layui-input-block">
      		<input type="text" name="activity_name" maxlength="60"  placeholder="活动名称最多可填写60个字符" lay-verify="required" value="${activity.activity_name!''}" autocomplete="off" class="layui-input">
    	</div>
  	</div>
	<div class="layui-form-item" style="width:100%;">
   		<label class="layui-form-label" >生效时间：</label>
    	<div class="layui-input-inline" >
      		<input class="layui-input" placeholder="活动开始日" id="add_time_from_index" name="startTime" value="<@dateformat time='${(activity.start_time)!0}' pattern='yyyy-MM-dd HH:mm:ss'/>" lay-verify="required" onclick="layui.laydate({elem: this, istime: true, format: 'YYYY-MM-DD hh:mm:ss'})" style="width:100%">
    	</div>
    	<div class="layui-input-inline">
      		<input class="layui-input" placeholder="活动截止日" id="add_time_to_index" name="endTime" value="<@dateformat time='${(activity.end_time)!0}' pattern='yyyy-MM-dd HH:mm:ss'/>" lay-verify="required" onclick="layui.laydate({elem: this, istime: true, format: 'YYYY-MM-DD hh:mm:ss'})" style="width:100%">
    	</div>                                                                                                
  	</div>
  	<div class="layui-form-item">
    	<label class="layui-form-label">活动描述：</label>
    	<div class="layui-input-block">
      		<script id="editor" name="description" type="text/plain" style="height:200px;width: 99%;">${activity.description!''}</script>
    	</div>
  	</div>
  	<#list detailList as detail>
  	<div class="layui-form-item">
		 <label class="layui-form-label" style="width:150px;">优惠门槛</label>
		 <div class="layui-input-inline" style="width:300px;margin:0;">
		     <span style="float:left;display:block;height:38px;line-height:38px;margin:0 10px">满</span><input name="full_money"  value="${detail.full_money!''}" placeholder="请输入优惠门槛金额" autocomplete="off" class="layui-input" type="text" style="float:left;width:70%;margin:0 10px;"><span style="float:left;display:block;height:38px;line-height:38px;">元</span>
		 </div>
	</div>
	<div class="layui-form-item" style="padding:0 0 30px 0;">
  	<input type="hidden" value="1" name="activity_type"/>
  	<label class="layui-form-label" style="width:150px;">优惠方式(可多选)</label>
  	
	  	<div class="layui-input-block" style="margin:0 0 0 150px;">
		<!-- 满减 -->
		<div class="same-acrivity-box">
			<input name="is_full_minus" title="减现金" value="1"  name="is_full_minus" type="checkbox" lay-filter="show" lay-skin="primary" <#if detail.is_full_minus == 1>checked="checked"</#if>>
			<div class="layui-input-inline same-acrivity-detail" style="width:300px;display:none;" >
				<span style="float:left;display:block;height:38px;line-height:38px;margin:0 10px">减</span>
				<input name="minus_value" id="minusValue" value="${detail.minus_value!''}" lay-verify="" placeholder="请输入优惠门槛金额" autocomplete="off" class="layui-input" type="text" style="float:left;width:70%;margin:0 10px;">
				<span style="float:left;display:block;height:38px;line-height:38px;">元</span>
			</div>
		</div>
		<!-- 送积分 -->
		<div class="same-acrivity-box">
		    <input name="is_send_point" title="送积分" value="1" type="checkbox" lay-filter="show" lay-skin="primary" <#if detail.is_send_point == 1>checked="checked"</#if>>
		    <div class="layui-input-inline same-acrivity-detail" style="width:300px;display:none;">
				 <span style="float:left;display:block;height:38px;line-height:38px;margin:0 10px">送</span><input name="point_value"  value="${detail.point_value!''}" placeholder="请输入积分数" autocomplete="off" class="layui-input" type="text" style="float:left;width:70%;margin:0 10px;"><span style="float:left;display:block;height:38px;line-height:38px;">分</span>
			</div>
		 </div>
		 <!-- 免邮费 -->
		 <div class="same-acrivity-box">
		      <input name="is_free_ship" title="免邮费" value="1" type="checkbox" lay-skin="primary" <#if detail.is_free_ship == 1>checked="checked"</#if>>
		 </div>
		 <!-- 送赠品 -->
		 <div class="same-acrivity-box">
		     <input name="is_send_gift" title="送赠品" value="1"  type="checkbox" lay-filter="show" lay-skin="primary" <#if detail.is_send_gift == 1>checked="checked"</#if>>
			 <div class="layui-inline same-acrivity-detail" style="display:none;">
				 <div class="layui-input-inline">
					  <select name="gift_id"  lay-search="">
							<option value="">请选择</option>
							<#if giftList??>
								<#list giftList as gift>			
									<option value="${gift.gift_id}" <#if detail.gift_id??><#if detail.gift_id == gift.gift_id>selected="selected"</#if></#if>>${gift.gift_name}</option>
								</#list>
							</#if>
					  </select>
				  </div>
				  <a href="javascript:;" onclick="newTab('赠品列表','${ctx}/shop/admin/gift/list.do')" style="color: blue;font-weight: bolder;font-size:small;margin-left: 10px;">去新增赠品</a>
			  </div>
  		  </div>
  		  <!-- 优惠券 -->
  		  <div class="same-acrivity-box">
		      <input name="is_send_bonus" title="赠送优惠券" value="1"  type="checkbox" lay-filter="show" lay-skin="primary" <#if detail.is_send_bonus == 1>checked="checked"</#if>>
		      <div class="layui-inline same-acrivity-detail" style="display:none;">
				  <div class="layui-input-inline">
					  <select name="bonus_id"  lay-search="">
						  <option value="">请选择</option>
						  <#if bonusList??>
						     <#list bonusList as bonus>
								<option value="${bonus.type_id}" <#if detail.bonus_id??><#if detail.bonus_id == bonus.type_id>selected="selected"</#if></#if>>${bonus.type_name}<#if bonus.send_type == 0> (电子券)</#if><#if bonus.send_type == 3> (实体券)</#if></option>
							 </#list>
						  </#if>
						</select>
				   </div>
				   <a href="javascript:;" onclick="newTab('优惠券列表','${ctx}/shop/admin/bonus-type/list.do')" style="color: blue;font-weight: bolder;font-size:small;margin-left: 10px;">去新增优惠券</a>
				</div>
  		    </div>
		  </div>	
  		</#list>
		<div style="margin:20px 0 0 0;">
			<h3 style="float: left;">选择活动商品</h3>
			<div style="float: left;width: 500px">
				<div style="margin:10px 0 0 20px;">
					<label>
						<input class="all-status" type="radio" value="1" title="全部商品参与"name="range_type" id="all" >
					</label>
				</div>
			</div>
			<div class="same-acrivity-box" style="margin:10px 0 0 10px;" >
				<label style="margin:10px 0 0 20px;">
					<input class="part-status" type="radio" value="2" title="部分商品参与" name="range_type" id="part">
					<!-- <a class='layui-btn layui-btn-small'>添加商品</a> -->
					<input type="hidden" value="0" id="hidInput">
				</label>
			</div>
			<div class="goodsTable" style="display: none; overflow-y: scroll;max-height:400px;width:100%;overflow-x:hidden;">
				 <table class="layui-table site-table table-hover" id="goodsTable" >
	                    <thead>
	                    	<tr>                    
	                            <th>缩略图</th>
	                            <th>商品名称</th>
	                            <th>商品编号</th>
	                            <th class="align-left">商品分类</th>
	                            <th>销售价格</th>
	                            <th>操作</th>
	                    	</tr>
	                    </thead>
	                    <tbody>

						</tbody>		
	               </table>
	         </div>
		</div>
		<div class="layui-form-item" style="margin:30px 0 0 0;" >
		    <div class="layui-input-block">
		       <button class="layui-btn" lay-submit="" lay-filter="edit" >立即提交</button>
		       <a onclick=""></a>
		    </div>
		 </div>
	</form>
</div>
<script type="text/javascript">
$(function(){
	//如果优惠方式的减现金复选框是选中状态
	if($("input[name='is_full_minus']").is(":checked")){
		$("input[name='is_full_minus']").parent().find(".same-acrivity-detail").show();
	}
	//如果优惠方式的送积分复选框是选中状态
	if($("input[name='is_send_point']").is(":checked")){
		$("input[name='is_send_point']").parent().find(".same-acrivity-detail").show();
	}
	//如果优惠方式的送赠品复选框是选中状态
	if($("input[name='is_send_gift']").is(":checked")){
		$("input[name='is_send_gift']").parent().find(".same-acrivity-detail").show();
	}
	//如果优惠方式的送优惠券复选框是选中状态
	if($("input[name='is_send_bonus']").is(":checked")){
		$("input[name='is_send_bonus']").parent().find(".same-acrivity-detail").show();
	}
	

	if(${activity.range_type}==1){
		$(".goodsTable").css('display','none');
		$("#all").attr("checked","checked");
	}else{
		$(".goodsTable").css('display','block');
		$("#part").attr("checked","checked");
	}
	var table = $('#goodsTable').DataTable({
		"language": {
	        "url": "${staticserver}/media/zh_CN.txt"
	    },
	    "bInfo": false,
	    "bPaginate": false,
	    "processing": true,
	    "serverSide": true,
	    "ordering": false,
	    "searching": false,
	    "lengthChange": false,
	    ajax: {
	        //指定数据源
	        type:"post",
	        url: '${ctx}/shop/admin/activity/list-goods-json-map.do?activity_id='+${activity.activity_id},
	    },
	    columns: [ //定义列        
            {"data": function (data) {
                return "<img src='"+ data.thumbnail +"' style='width: 40px; height: 40px;' /><input type='hidden' value="+data.goods_id+" name='goods_ids' class='goodsid' />";
            }},
            {"data": "name"},
            {"data": "sn"},
            {"data": "cat_name"},
            {"data": "price"},
            {"data": function (data) {
                return "<a class='layui-btn layui-btn-small clearon'  onclick='del(this)' >删除</a>";
            }},
	     ]
	});
	
	
})



var ue = UE.getEditor('editor');

var index = parent.layer.getFrameIndex(window.name);

layui.use(['form', 'layedit', 'laydate'], function(){
    var form = layui.form()
        ,layer = layui.layer
        ,layedit = layui.layedit
        ,laydate = layui.laydate;
    
       form.render(); //更新全部
    
		  form.on('checkbox(show)', function(data){
			 var isShow = data.elem.checked; //开关是否开启，true或者false
			 if(isShow == true){
				 $(this).parents(".same-acrivity-box").find(".same-acrivity-detail").show();
			 }else{
				 $(this).parents(".same-acrivity-box").find(".same-acrivity-detail").hide();
			 }
		});

			//全部商品参加单选框点击操作
			$(".all-status").click(function(){
				$(".goodsTable").css('display','none');
			});
			//部分商品参加单选框点击操作
			$(".part-status").click(function(){
				$(".goodsTable").css('display','block');
				
				//当id=hidInput这个input框的值等于0的时候，就让商品选择器加载商品并将input框的值设置为1，以防止再次加载
				if($("#hidInput").val() == 0){
					  $.GoodsSelector({
                          api: '/shop/admin/activity/list-goods-json-map.do?activity_id=0',
                          confirm: function (goodsArray) {
				        	  //遍历
				               $.each(goodsArray,function(n,values){			            	  	        
				             		var data = piece(values);
				             		var num = $(".goodsTable tbody").find("tr").length;		             
				             		if(num>0){
				             			var num = 0;
				             			$(".goodsTable tbody").find("input[name=goods_ids]").each(function(index,value){
				            
				             				if($(value).val()==values.goods_id){	  
						             			num = 1;
						             		}
					             		});	             			
				             			if(num==0){
				             				$("#goodsTable tbody").prepend(data);
				             			}
				             		}else{
				             			$("#goodsTable tbody").prepend(data);
				             		}
				               })
				               //删除行
				               $(".clearon").click(function(){		            	
				           	   		$(this).parent().parent().remove();
				           		})	
				               
				         }
				});
				}
			});
		        
    //监听提交
    form.on('submit(edit)', function(){
    	var options = {
    		url : "${ctx}/shop/admin/activity/save-edit.do?ajax=yes",
   			type:"post",
   			dataType:"json",
   			success:function(data){
   				if(data.result==1){	
   					parent.$.Loading.success(data.message);
   					closeCurrent();//再执行关闭       
   				}
   				if(data.result==0){
   					parent.$.Loading.error(data.message);
   				}
   			}
   		};
   		$("#edit_form").ajaxSubmit(options);
       return false;
    });
    
});
//拼接
function piece(data){
	var value= "<tr>"+ 		  
		  "<td ><img src='"+ data.thumbnail +"' style='width: 40px; height: 40px;' /><input type='hidden' value="+data.goods_id+" name='goods_ids' id='goods_ids' class='goodsid' /></td>"+
		  "<td >"+data.name+"</td>"+
		  "<td >"+data.sn+"</td>"+
		  "<td >"+data.cat_name+"</td>"+
		  "<td >"+data.price+"</td>"+
		  "<td ><a class='layui-btn layui-btn-small clearon' >删除</a></td>"+
		  "</tr>";
     return value;
}
//删除
function del(obj){
	
	$(obj).parent().parent().remove()
}

</script>
<#include '/admin/footer.html' >